//
//  AEAppDelegateTool.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/13.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEAppDelegateTool.h"
#import "TYZNetworkConfig.h"
#import "AEUserInfoRequest.h" // 获取用户信息
#import "AE_Solar-Swift.h"
#import "TYZNavRootVCInfo.h"
#import "AEHomePageViewController.h"

@interface AEAppDelegateTool () <UITabBarControllerDelegate>

@end

@implementation AEAppDelegateTool

+ (instancetype)sharedInstance {
    static AEAppDelegateTool *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[[self class] alloc] init];
    });
    return instance;
}

- (instancetype)init {
    if (self = [super init]) {
        [self setup];
    }
    return self;
}

- (void)setup {
    [self setupUUID];
    
    [TYZNetworkConfig sharedConfig].baseUrl = kBaseUrl;
    
    // 用户信息
    id userInfo = [TYZKeychain load:kUserInfo];
    if (userInfo) {
        self.userInfoModel = userInfo;
    }
    
    [self setSvProgressHUD];
    
    [self requestWithUserInfo:^(EDSRespondDataEntity * _Nonnull respond) {
        
    }];
}

/**
 唯一标识码
 */
- (NSString *)setupUUID
{
    NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    NSString *uuid = [TYZKeychain load:bundleId];
    if ([objectNull(uuid) isEqualToString:@""])
    {
        uuid = [NSString stringWithUUID];
        [TYZKeychain save:bundleId data:uuid];
    }
    return uuid;
}

- (NSString *)deviceId
{
    return [self setupUUID];
}

- (void)setSvProgressHUD
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
    [SVProgressHUD setMinimumDismissTimeInterval:1.5];
}

/// 保存用户信息
/// - Parameter userInfo: userInfo description
- (void)saveUserInfo:(AEUserInfoModel *)userInfo {
    self.userInfoModel = userInfo;
    [TYZKeychain save:kUserInfo data:userInfo];
}

/// 获取用户信息
- (AEUserInfoModel *)getUserInfo {
    return self.userInfoModel;
}

- (NSString *)getUUID {
    NSString *bundleId = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
    NSString *uuid = [TYZKeychain load:bundleId];
    if (!uuid) {
        uuid = [NSString stringWithUUID];
        [TYZKeychain save:bundleId data:objectNull(uuid)];
    }
    return uuid;
}

/// 删除用户信息
- (void)removeUserInfo {
    [TYZKeychain remove:kUserInfo];
    self.userInfoModel = nil;
}

/// 获取用户信息
/// - Parameter completion: completion description
- (void)requestWithUserInfo:(RequestCompletionBlock)completion {
    @weakify(self);
    NSString *userId = [AEUserCommonInfo getUserId];
    AEUserInfoRequest *request = [[AEUserInfoRequest alloc] initWithUserId:userId];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self saveUserInfo:respond.data];
        }
        if (completion) {
            completion(request.responseModelObject);
        }
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        if (completion) {
            completion(request.responseModelObject);
        }
    }];
}

/// 到主视图
- (TYZCustomTabBarController *)getMainTabBarVC {
    // 首页
    NSString *title = [AEStringExten localizedValueWithKey:@"首页"];
    
    TYZPageConfigration *config = [AEHomePageViewController setupConfig];
    
    NSArray *titles = [AEHomePageViewController setupMenuTitles];
    NSArray *subVcs = [AEHomePageViewController setupViewControllers:@{}];
    AEHomePageViewController *rootVC = [[AEHomePageViewController alloc] initPageWithControllers:subVcs titles:titles config:config];

    TYZNavRootVCInfo *mainInfo = [[TYZNavRootVCInfo alloc] initWithInfo:title clsName:nil imgNorName:@"ic_tab_main_nor" imgSelName:@"ic_tab_main_sel"];
    mainInfo.vc = rootVC;//[[HomePageViewController alloc] init];
    
    // 检验
    title = [AEStringExten localizedValueWithKey:@"检验"];
    TYZNavRootVCInfo *checkInfo = [[TYZNavRootVCInfo alloc] initWithInfo:title clsName:nil imgNorName:@"ic_tab_check_nor" imgSelName:@"ic_tab_check_sel"];
    checkInfo.vc = [[CheckViewController alloc] init];
    
    // 积分
    title = [AEStringExten localizedValueWithKey:@"积分"];
    TYZNavRootVCInfo *pointsInfo = [[TYZNavRootVCInfo alloc] initWithInfo:title clsName:nil imgNorName:@"ic_tab_points_nor" imgSelName:@"ic_tab_points_sel"];
    pointsInfo.vc = [[AEPointsMallViewController alloc] init];
    
    // 我的
    title = [AEStringExten localizedValueWithKey:@"我的"];
    TYZNavRootVCInfo *mineInfo = [[TYZNavRootVCInfo alloc] initWithInfo:title clsName:nil imgNorName:@"ic_tab_mine_nor" imgSelName:@"ic_tab_mine_sel"];
    mineInfo.vc = [[AEPersonViewController alloc] init];
    
    TYZCustomTabBarController *rootTabBar = [[TYZCustomTabBarController alloc] initWithShowMiddleBtn:NO midNorImgName:nil midSelImgName:nil];
    rootTabBar.delegate = self;
    NSArray *vcs = @[mainInfo, checkInfo, pointsInfo, mineInfo];
    
    TYZNavInfo *navInfo = [[TYZNavInfo alloc] initWithNavBackgroundColor:kNavBarBackgroundColor titleFont:kNavBarTitleFont titleColor:kNavBarTitleColor];
    
    [rootTabBar setupChildViewController:vcs navInfo:navInfo];
    [rootTabBar setupTabBarItemTextAttributes:kTabBarItemTitleNorColor selTitleColor:kTabBarItemTitleSelColor titleFont:kTabBarItemTitleFont];
//    kThemeColor
    return rootTabBar;
}

@end
